vim.api.nvim_create_autocmd({ "VimLeave" }, { command = "mksession! ~/.config/nvim_session.vim" })
-- vim.api.nvim_create_autocmd({ "VimEnter" }, { command = "colorscheme github_light" })
-- vim.api.nvim_create_autocmd({ "VimEnter" }, {
-- once = true,
-- nested = false,
-- callback = function()
-- vim.cmd("colorscheme github_light")
-- end,
-- })
vim.api.nvim_create_autocmd("VimEnter", {
    callback = function()
        vim.notify("Start NeoVim!", vim.log.levels.INFO, { title = "nvim" })
    end,
})

vim.api.nvim_create_autocmd("LspAttach", {
    callback = function(ev)
        local client = vim.lsp.get_client_by_id(ev.data.client_id)
        if client:supports_method("textDocument/completion") then
            vim.lsp.completion.enable(true, client.id, ev.buf, { autotrigger = true })
        end
    end,
})

vim.diagnostic.config({ virtual_lines = true })

-- vim.cmd([[autocmd FocusGained, * checktime]])
vim.api.nvim_create_autocmd("FileType", {
    pattern = {
        "qf",
        "man",
        "help",
        "Trans",
        "notify",
        "lspinfo",
        "coctree",
        "translator",
        "spectre_panel",
        "startuptime",
        "tsplayground",
        "PlenaryTestPopup",
    },
    callback = function(event)
        vim.bo[event.buf].buflisted = false
        vim.keymap.set("n", "q", "<cmd>close<cr>", { buffer = event.buf, silent = true })
    end,
})

vim.api.nvim_create_autocmd("FileType", {
    pattern = { "man" },
    callback = function()
        vim.opt.number = true
    end,
})

vim.api.nvim_create_autocmd("FileType", {
    pattern = { "c" },
    callback = function()
        vim.opt_local.makeprg = "gcc -g -std=c99 %"
    end,
})

vim.api.nvim_create_autocmd("FileType", {
    pattern = { "cpp" },
    callback = function()
        vim.opt_local.makeprg = "g++ -g -std=c++17 %"
    end,
})

vim.api.nvim_create_autocmd({ "QuickFixCmdPost" }, {
    desc = "Automatically open quickfix on :make",
    command = "cwindow",
    pattern = "[^l]*",
    nested = true,
})

vim.api.nvim_create_autocmd("BufEnter", {
    callback = function()
        if vim.opt.filetype:get() == "help" then
            vim.wo.number = true
        end
    end,
})

vim.api.nvim_create_autocmd({ "FocusGained" }, { command = "checktime" })

-- Highlight Yanked Text
vim.api.nvim_create_autocmd({ "TextYankPost" }, {
    -- command = "silent! lua vim.highlight.on_yank()",
    callback = function()
        vim.highlight.on_yank({ higroup = "Visual", timeout = 200 })
    end,
})

-- vim.api.nvim_create_autocmd({ "FileChangedShellPost" }, {
-- pattern = "*",
-- callback = function()
-- vim.notify("File changed on disk. Buffer reloaded!", vim.log.levels.WARN, { title = "nvim-config" })
-- end,
-- })

vim.api.nvim_create_autocmd({ "BufNewFile", "BufEnter" }, {
    desc = "Add a defaualt gitignore configuration",
    pattern = { "*.gitignore" },
    callback = function()
        local gitignore = {
            "#basic",
            "*.pdf",
            "*.doc",
            "*.docx",
            "*.txt",
            "*.html",
            "*.jpg",
            "*.chm",
            "*.log",
            "*.xml",
            "*.lua_",
            ".cache",
            ".vscode",
            ".nvim",
            ".metals",
            ".idea",
            "tmp",
            "bin",
        }
        if vim.fn.bufname() ~= ".gitignore" then
            return
        end
        local bufnr = vim.api.nvim_win_get_buf(vim.api.nvim_get_current_win())
        local count = vim.api.nvim_buf_line_count(bufnr)
        if count <= 2 then
            vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, gitignore)
        end
    end,
})

vim.api.nvim_create_autocmd({ "BufNewFile", "BufEnter" }, {
    desc = "Add a defaualt clang-format configuration",
    pattern = { "*.clang-format" },
    callback = function()
        local clang_format = {
            "Language                                       : Cpp         ",
            "BasedOnStyle                                   : Google      ",
            "AlignArrayOfStructures                         : Left        ",
            "AccessModifierOffset                           : -4          ",
            "AlignConsecutiveAssignments                    : true        ",
            "AlignConsecutiveBitFields                      : Consecutive ",
            "AlignConsecutiveDeclarations                   : Consecutive ",
            "AlignConsecutiveMacros                         : Consecutive ",
            "AlignEscapedNewlines                           : Left        ",
            "AllowAllArgumentsOnNextLine                    : true        ",
            "AllowShortBlocksOnASingleLine                  : false       ",
            "AllowShortCaseLabelsOnASingleLine              : false       ",
            "AllowShortEnumsOnASingleLine                   : false       ",
            "AllowShortFunctionsOnASingleLine               : None        ",
            "AllowShortIfStatementsOnASingleLine            : false       ",
            "AllowShortLambdasOnASingleLine                 : Empty       ",
            "AllowShortLoopsOnASingleLine                   : false       ",
            "AlwaysBreakTemplateDeclarations                : true        ",
            "BreakBeforeBraces                              : Allman      ",
            "BreakBeforeInheritanceComma                    : false       ",
            "BreakConstructorInitializers                   : AfterColon  ",
            "BreakConstructorInitializersBeforeComma        : false       ",
            "BreakInheritanceList                           : AfterColon  ",
            "LambdaBodyIndentation                          : OuterScope  ",
            "ColumnLimit                                    : 0           ",
            "ConstructorInitializerAllOnOneLineOrOnePerLine : true        ",
            "ConstructorInitializerIndentWidth              : 4           ",
            "IndentWidth                                    : 4           ",
            "PackConstructorInitializers                    : Never       ",
            "MaxEmptyLinesToKeep                            : 1           ",
            "SortIncludes                                   : false       ",
            "SpaceBeforeCtorInitializerColon                : true        ",
            "SpaceBeforeParens                              : Never       ",
            "Standard                                       : C++11       ",
            "UseTab                                         : Never       ",
        }
        if vim.fn.bufname() ~= ".clang-format" then
            return
        end
        local bufnr = vim.api.nvim_win_get_buf(vim.api.nvim_get_current_win())
        local count = vim.api.nvim_buf_line_count(bufnr)
        if count <= 5 then
            vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, clang_format)
        end
    end,
})
